3 Lecture

CS401

Midterm & Final Term Short Notes

Comparison and Conditions

Comparison and conditions are fundamental concepts in programming that involve evaluating expressions and executing code based on the result. Comparisons are made using relational operators like <, >, ==, <=, >=, and != to determine if two value


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which operator is used to check for equality in programming languages? a) = b) == c) === d) != Answer: b) == Which of the following is a relational operator? a) ! b) || c) && d) > Answer: d) > What is the result of the following comparison in Python: 5 > 3? a) True b) False c) Syntax Error d) None of the above Answer: a) True Which of the following is not a logical operator? a) ! b) || c) <= d) && Answer: c) <= What is the output of the following code: arduino Copy code int a = 5; if (a < 10) { printf("a is less than 10"); } a) a is less than 10 b) a is greater than 10 c) a is equal to 10 d) Compilation Error Answer: a) a is less than 10 What is the output of the following code: css Copy code int a = 10; if (a > 5 && a < 15) { printf("a is between 5 and 15"); } a) a is between 5 and 15 b) a is less than 5 c) a is greater than 15 d) Compilation Error Answer: a) a is between 5 and 15 Which of the following control structures is used to execute code based on multiple conditions? a) if-else b) switch-case c) while d) for Answer: b) switch-case Which of the following is the correct syntax for an if-else statement in C? a) if (condition) { // code block } else { // code block } b) if (condition) // code block else // code block c) if (condition) // code block else { // code block } d) if (condition) { // code block } else // code block Answer: a) if (condition) { // code block } else { // code block } What is the output of the following code: css Copy code int a = 5; int b = 3; if (a > b) { printf("a is greater than b"); } else { printf("a is less than or equal to b"); } a) a is greater than b b) a is less than or equal to b c) a is equal to b d) Compilation Error Answer: a) a is greater than b Which of the following operators is used to check for inequality? a) ! b) == c) != d) <> Answer: c) !=


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a comparison operator? Give an example. Answer: A comparison operator is used to compare two values and return a Boolean value (true or false). Example: the greater than operator (>), which checks if one value is greater than another. What is the difference between the logical AND operator (&&) and the logical OR operator (||)? Answer: The logical AND operator returns true if both conditions are true, while the logical OR operator returns true if at least one of the conditions is true. What is a conditional statement? Give an example. Answer: A conditional statement is a programming construct that executes different code blocks based on the evaluation of a Boolean expression. Example: an if-else statement that checks if a variable is greater than 10 and executes different code blocks accordingly. What is short-circuit evaluation in programming? Answer: Short-circuit evaluation is a behavior of logical operators in which the second operand is not evaluated if the result of the expression can be determined by the first operand. This can improve performance and avoid errors. What is the syntax of a switch-case statement? Answer: switch (expression) { case value1: // code block1 break; case value2: // code block2 break; default: // code block3 } What is the difference between the equal to operator (==) and the assignment operator (=)? Answer: The equal to operator compares two values for equality, while the assignment operator assigns a value to a variable. What is the purpose of the ternary operator in programming? Answer: The ternary operator is a shorthand way of writing an if-else statement that returns a value. It can improve code readability and reduce lines of code. What is a truthy value in programming? Answer: A truthy value is a value that is considered true in a Boolean context, even if it is not explicitly true. Examples include non-zero numbers and non-empty strings. What is the difference between the not equal to operator (!=) and the strict not equal to operator (!==)? Answer: The not equal to operator compares two values for inequality, while the strict not equal to operator compares both the value and the type of the values for inequality. What is the order of precedence of logical operators in programming? Answer: The order of precedence of logical operators is NOT, AND, OR.

Comparison and conditions are essential concepts in programming. They allow developers to create programs that can make decisions based on specific criteria. Comparison operators are used to compare two values and return a Boolean value (true or false). Some commonly used comparison operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators are used to evaluate conditions and make decisions based on them. Logical operators, such as AND (&&), OR (||), and NOT (!), are used to combine multiple conditions into a single expression. The AND operator returns true if both conditions are true, while the OR operator returns true if at least one of the conditions is true. The NOT operator is used to negate the value of a Boolean expression. Conditional statements, such as if-else statements and switch-case statements, are used to execute different code blocks based on the evaluation of a Boolean expression. For example, an if-else statement can be used to check if a variable is greater than 10 and execute different code blocks accordingly. Short-circuit evaluation is a behavior of logical operators in which the second operand is not evaluated if the result of the expression can be determined by the first operand. This can improve performance and avoid errors. The ternary operator is a shorthand way of writing an if-else statement that returns a value. It can improve code readability and reduce lines of code. It is important to understand the order of precedence of logical operators in programming. The order of precedence is NOT, AND, OR. This means that the NOT operator is evaluated first, followed by the AND operator, and finally the OR operator. In summary, comparison and conditions are essential concepts in programming that allow developers to create programs that can make decisions based on specific criteria. By using comparison operators, logical operators, and conditional statements, developers can create robust and efficient programs that can handle complex tasks.